Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: TimeWithzone bug #40448

Merged
merged 1 commit into from
Oct 30, 2020
Merged

Conversation

benkoshy
Copy link
Contributor

@benkoshy benkoshy commented Oct 25, 2020

Summary

What was the problem?

There was a rounding off issue here when we were comparing TimeWithZone times with DateTime raised in #40413. I have written a failing test and made it pass.

How did we fix it?

The problem was that when we using time_instance.to_f - this was not accurate enough in certain cases. I have attempted to solve it by ensuring that we use Rationals when creating Time instances time_instance.to_r . (there is a slight performance cost to this, but the benefit is accuracy).

Thanks for reviewing and I hope this helps.

rgds,
Ben

@benkoshy benkoshy changed the title WIP: fix timezone bug Fix: timezone bug Oct 25, 2020
Why this commit?

   TimeWithZone instances were unexpectedly being rounded up.

What changes were made and why?

   Internally, .to_f was being called on TimeWithZone instances.
This can lead to inaccuracies if Rationals are involved. Using .to_r
instead of .to_f will be more accurate, but it does come with a
slight computational cost.
@@ -47,7 +47,9 @@ def at_with_coercion(*args)
# Time.at can be called with a time or numerical value
time_or_number = args.first

if time_or_number.is_a?(ActiveSupport::TimeWithZone) || time_or_number.is_a?(DateTime)
if time_or_number.is_a?(ActiveSupport::TimeWithZone)
at_without_coercion(time_or_number.to_r).getlocal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the alternative implementation I'm pushing for in #40137. #40413 is a duplicate of #38831, although demonstrated slightly differently.

I'd like to wait a bit and see if #40137 gets merged, since it was opened first and the author clearly invested a lot of time into it already.

@benkoshy benkoshy changed the title Fix: timezone bug Fix: TimeWithzone bug Oct 25, 2020
@eugeneius eugeneius merged commit a6636b0 into rails:master Oct 30, 2020
@eugeneius
Copy link
Member

I went ahead with this to ensure that #40413 is fixed in the next release. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants